<?php
class food_option_category_model_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        $this->load->model('food_option_category_model', 'food_option_category');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        // get category function
        $rows = rand(1,4);
        $categories_test = $this->food_option_category->get_category($rows);
        $this->unit->run(count($categories_test), $rows, "categories retrieval test");

        // $id, $en_name, $ch_name, $sequence
        //need two v testing data since we have swap function, C -> U -> Swap -> D, R as helper
        $iv_create_data = array("", "", "asdf", "really");
        $v_create_data1 = array("en_name_test1", "ch_name_test2", "52-46", "quick");
        $v_create_data2 = array("en_name_test1", "ch_name_test2", "32-36", "all");
        // C time
        $iv_create_test = $this->food_option_category->create($iv_create_data[0], $iv_create_data[1], $iv_create_data[2], $iv_create_data[3]);
        $v_create_test1 = $this->food_option_category->create($v_create_data1[0], $v_create_data1[1], $v_create_data1[2], $v_create_data1[3]);
        $v_data1_id = $this->db->insert_id();
        $v_create_test2 = $this->food_option_category->create($v_create_data2[0], $v_create_data2[1], $v_create_data2[2], $v_create_data2[3]);
        $v_data2_id = $this->db->insert_id();
        
        $this->unit->run(count($iv_create_test['user']), 4, "iv data creation");
        $this->unit->run(count($v_create_test1['user']), 0, "v data1 creation");
        $this->unit->run(count($v_create_test2['user']), 0, "v data2 creation");

        // U time
        $iv_update_data = array("", "", "you suck", "right");
        $v_update_data = array("update", "genxin", "99-9", "quick");
        $iv_update_test = $this->food_option_category->update($v_data1_id, $iv_update_data[0], $iv_update_data[1], $iv_update_data[2], $iv_update_data[3]);
        $v_update_test = $this->food_option_category->update($v_data1_id, $v_update_data[0], $v_update_data[1], $v_update_data[2],$v_update_data[3]);
        $v_confirm_update = $this->food_option_category->read($v_data1_id);

        $this->unit->run(count($iv_update_test['user']), 4, "iv data update");
        $this->unit->run(count($v_update_test['user']), 0, "v data update");
        $this->unit->run($v_confirm_update['sequence'], $v_update_data[2], "confirm data update");

        //Swap time
        $same_place_swap = $this->food_option_category->swap(1,1);
        $this->food_option_category->swap($v_data1_id, $v_data2_id);
        $data1 = $this->food_option_category->read($v_data1_id);
        $data2 = $this->food_option_category->read($v_data2_id);
        $this->unit->run($same_place_swap['user']['swap_sql'], "swap between same place disallowed", "swap between same place test");
        $this->unit->run($data1['sequence'], "32-36", "data 1 swap confirm");
        $this->unit->run($data2['sequence'], "99-9", "data 2 swap confirm");

        // D time
        $iv_delete = $this->food_option_category->delete("invalid_id");
        $this->unit->run(array_key_exists('delete_query', $iv_delete['system']), true ,"invliad id delete test");

        $iv_group_delete = $this->food_option_category->delete("2");
        $this->unit->run(array_key_exists('delete_non_empty_category', $iv_group_delete['user']), true ,"invliad group delete test");

        $this->food_option_category->delete($v_data1_id);
        $this->food_option_category->delete($v_data2_id);
        $non_exsit1 = $this->food_option_category->read($v_data1_id);
        $non_exsit2 = $this->food_option_category->read($v_data2_id);
        $this->unit->run(count($non_exsit1), 0, "delete data1 confirm");
        $this->unit->run(count($non_exsit2), 0, "delete data2 confirm");

        $this->_report();
    }
    
    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }

}